var util = require('../../utils/util.js');
var refresh = false;
Page({
  data: {
    version: 'V1.5.1',
    dfh: 0,
    yfh: 0,
    ysh: 0
  },
  onLoad: function (options) {
    refresh = options.refresh || false;
    var that = this;
    wx.showLoading({
      title: '数据请求中',
    });
    if (refresh) {
      that.searchOrders();
    } else {
      if (util.get('dfh', 'outTime') == 'outTime') {
        that.searchOrders();
      } else {
        console.log('【我的】页面数据调用缓存。');
        wx.hideLoading();
        that.setData({
          dfh: wx.getStorageSync('dfh'),
          yfh: wx.getStorageSync('yfh'),
          ysh: wx.getStorageSync('ysh'),
          order_list: wx.getStorageSync('order_list')
        })
      }
    }
  },
  // 查询订单数据
  searchOrders() {
    var that = this;
    let times = 120;
    wx.cloud.callFunction({
      name: "OrderFunctions",
      data: {
        type: 'myOrder',
        user_openid: wx.getStorageSync('openid')
      },
      success: res => {
        wx.hideLoading();
        console.log(res);
        that.setData({
          dfh: res.result[0].total,
          yfh: res.result[1].total,
          ysh: res.result[2].total,
          order_list: res.result[3].list
        });
        // 缓存times秒
        util.put('dfh', res.result[0].total, times);
        util.put('yfh', res.result[1].total, times);
        util.put('ysh', res.result[2].total, times);
        util.put('order_list', res.result[3].list, times);
      }
    })
  },
  // 下拉刷新
  onPullDownRefresh: function () {
    this.onLoad(refresh);
    wx.stopPullDownRefresh();
  },
  // 跳转到快递信息页面
  toExpress(e) {
    let express_id = e.currentTarget.dataset.express;
    let express_name = '';
    if (express_id != null) {
      wx.navigateTo({
        url: '../customer/express/express?express_id=' + express_id + '&express_name=' + express_name,
      })
    } else {
      wx.showToast({
        title: '待发货',
        icon: 'error'
      })
    }
  },
  // 复制项目地址
  copyAddress() {
    wx.setClipboardData({
      data: 'https://gitee.com/daixiangcn/ops',
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            wx.showToast({
              title: '项目网址已复制',
              icon: 'success'
            })
          }
        })
      }
    })
  },
  // 清空缓存
  clearStorage() {
    wx.showModal({
      title: '清空缓存',
      content: '确定清空小程序缓存吗？',
      success: res => {
        if (res.confirm) {
          wx.clearStorage({
            success: (res) => {
              wx.showToast({
                title: '已清空缓存',
                icon: 'success'
              }, 2000);
              // 延时跳转
              setTimeout(function () {
                wx.switchTab({
                  url: '../index/index',
                })
              }, 2000)
            },
          })
        }
      }
    })
  }
})